
\def\eps{\textsc{eps}}

\input{pgfmanual-en-macros.tex}


\def\pgfplotsifdocpackageuptodate#1#2{%
    \pgfkeysifdefined{/codeexample/prettyprint/word/.@cmd}{#1}{#2}
}%

\pgfplotsiffileexists{pgfmanual.sty}{%
    \RequirePackage{pgfmanual}
    \pgfplotsifdocpackageuptodate{}{%
        \makeatletter
        \input{pgfplotsoldpgfsupp_pgfmanual.code.tex}
        \makeatother
    }%
}{%
    \makeatletter
    \input{pgfplotsoldpgfsupp_pgfmanual.code.tex}
    \makeatother
}%

\makeatletter
\def\pgfplotsmakefilelinkifuseful#1#2{%
    \protect\pgfplotsmakefilelinkifuseful@{#1}{#2}%
}%
\def\pgfplotsmakefilelinkifuseful@#1#2{%
    \edef\temp{#1}%
    \edef\tempb{\jobname}%
    \edef\temp{\meaning\temp}% \meaning normalizes the catcodes.
    \edef\tempb{\meaning\tempb}%
    \ifx\temp\tempb
        % we are processing '#1'. Don't make a link.
        #2%
    \else
        \href{file:#1.pdf}{#2}%
    \fi
}%
\makeatother


\pgfkeys{
    /codeexample/prettyprint/cs arguments/pgfplotscreateplotcyclelist/.initial=2,
    /codeexample/prettyprint/cs/pgfplotscreateplotcyclelist/.code args={#1#2#3}{\pgfmanualpdfref{#1}{#1}\{#2\}\{\pgfmanualprettyprintpgfkeys{#3}\pgfmanualclosebrace},
    /codeexample/prettyprint/cs arguments/tikzset/.initial=1,
    /codeexample/prettyprint/cs/tikzset/.code 2 args={\pgfmanualpdfref{#1}{#1}\{\pgfmanualprettyprintpgfkeys{#2}\pgfmanualclosebrace},
    /codeexample/prettyprint/cs arguments/pgfplotsset/.initial=1,
    /codeexample/prettyprint/cs/pgfplotsset/.code 2 args={\pgfmanualpdfref{#1}{#1}\{\pgfmanualprettyprintpgfkeys{#2}\pgfmanualclosebrace},
    /codeexample/prettyprint/cs arguments/pgfplotstableset/.initial=1,
    /codeexample/prettyprint/cs/pgfplotstableset/.code 2 args={\pgfmanualpdfref{#1}{#1}\{\pgfmanualprettyprintpgfkeys{#2}\pgfmanualclosebrace},
    /codeexample/prettyprint/cs arguments/usepgfplotslibrary/.initial=1,
    /codeexample/prettyprint/cs/usepgfplotslibrary/.code 2 args={\pgfmanualpdfref{#1}{#1}\{\pgfmanualpdfref{#2}{#2}\pgfmanualclosebrace},
    %
    %
    %/codeexample/prettyprint/key value/cycle list/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/xticklabel/.code 2 args={\pgfmanualprettyprintcode{#2}},
    /codeexample/prettyprint/key value/yticklabel/.code 2 args={\pgfmanualprettyprintcode{#2}},
    /codeexample/prettyprint/key value/zticklabel/.code 2 args={\pgfmanualprettyprintcode{#2}},
    /codeexample/prettyprint/key value/nodes near coords/.code 2 args={\pgfmanualprettyprintcode{#2}},
    /codeexample/prettyprint/key value/nodes near coords*/.code 2 args={\pgfmanualprettyprintcode{#2}},
    %
    /codeexample/prettyprint/key value/includegraphics/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/contour gnuplot/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/contour lua/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/contour prepared/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/contour filled/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/hist/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/tieline/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/quiver/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/boxplot/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/boxplot prepared/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    %
    /codeexample/prettyprint/key value/colormap/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/of colormap/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/samples of colormap/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/levels from colormap/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    /codeexample/prettyprint/key value/colors of colormap/.code 2 args={\pgfmanualprettyprintpgfkeys{#2}},
    %
    %
    % whenever an unqualified key is found, the following key prefix
    % list is tried to find a match.
    /pdflinks/search key prefixes in={/pgfplots/table/,/pgfplots/error bars/,/pgfplots/,/pgfplots/plot file/,/tikz/,/pgf/},
    %
    % the link prefix written to the pdf file:
    /pdflinks/internal link prefix=pgfp,
    %
    /pdflinks/warnings=false,
    /pdflinks/codeexample links=true,
    /pdflinks/show labels=false,
}%


% should be used to show something in red which doesn't need to get a
% hyper ref.
%
% Examples are descriptions of key labels.
\def\declaretext#1{\texttt{\declare{#1}}}

% To be used whenever something NEW has been declared.
% In this case, a \pgfmanualpdflabel will be generated using '#1'.
%
% Use '\declaretext' if you only describe something local (for example
% the documentation of key values).
\def\declarelabel#1{%
    \texttt{\declare{#1}}%
    \pgfmanualpdflabel{#1}{}%
}

\def\pgfmanualbar{\char`\|}
\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepgfplotslibrary{external}

% use \pgfplotsmanualenableexternalizationofexpensive in the preamble
% to enable externalization of expensive examples.
% This will ONLY externalize expensive examples, i.e. those for which
% \pgfplotsexpensiveexample is written:
%
% \pgfplotsexpensiveexample
% \begin{codeexample}
\def\pgfplotsmanualenableexternalizationofexpensive{%
    \pgfplotsmanual@enable@externalization@for@expensivetrue
    \tikzexternalize[
        prefix=figures/expensiveexampleX,% the 'X' suffix is to avoid confusion in git: previous versions contained the pdfs in git
        figure name={},
        export=false, % needs to be activated for single pictures (i.e. expensive ones)
        mode=list and make,
        verbose=false,
        %xport=true,% FASTER FOR DEBUGGING
    ]
    \tikzifexternalizing{%
        \nofiles
        \pgfkeys{/pdflinks/codeexample links=false}%
    }{}%
}%
%\pgfkeys{/pgf/images/include external/.code={\href{file:#1}{\pgfimage{#1}}}} % FIXME : NOT FOR THE FINAL VERSION

\newif\ifpgfplotsmanual@enable@externalization@for@expensive
\newif\ifpgfplots@example@is@expensive

\pgfkeys{
    /codeexample/every codeexample/.append code={%
        \ifpgfplots@example@is@expensive
            \pgfkeys{/tikz/external/export=true}%
            \global\pgfplots@example@is@expensivefalse
        \fi
    }
}

% Write this macro directly in front of \begin{codeexample} (without arguments):
\def\pgfplotsexpensiveexample{%
    \ifpgfplotsmanual@enable@externalization@for@expensive
        \pgfplots@example@is@expensivetrue
    \else
        \message{[NOTE: I am now about to typeset an expensive example. You will need to ENLARGE YOUR TeX MEMORY CAPACITIES if this fails.]}%
    \fi
}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newif\ifpgfplotsmanualhtmlmode

% MACROS FOR HTML OUTPUT:
% HTML will be compiled in a separate folder. See the Makefile
\tikzset{
    external/html export mode/.style={
        system call={%
        % FIXME : which converter to png ? Why did I use pdftoppm |
        % pnmtopng in the first draft?
            %pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\\def\\pgfplotsmanualhtlatexmode{1}\texsource" && pdftoppm "\image.pdf" | pnmtopng > "\image.png"%
            pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\\def\\pgfplotsmanualhtlatexmode{1}\texsource" && pdftoppm -png "\image.pdf" "\image"%
            %pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\\def\\pgfplotsmanualhtlatexmode{1}\texsource" && convert -density 150 "\image.pdf" "\image.png"%
        },
        /pgf/images/external info,
        /pgf/images/include external/.code={%
            \includegraphics
               [width=\pgfexternalwidth,height=\pgfexternalheight]
               {##1.png}%
        },
        prefix=figures/generated/,
        figure name={manual},
        mode=list and make,
        verbose=false,
    },
}

\def\pgfplotsmanual@configure@for@htlatexmode{%
    % disable this; we will externalize everything now:
    \let\pgfplotsmanualenableexternalizationofexpensive=\relax
    %
    \message{^^Jpgfplots manual: initializing external lib for HTML output (png)^^J}%
    \tikzexternalize[
        html export mode,
    ]
    %
    \@ifpackageloaded{tex4ht}{%
        %\usepackage[html,png,3]{tex4ht}
        \tikzset{external/mode=only graphics}%
        \pgfplotsmanual@configure@for@htlatexmode@
        %\patches@for@htlatex
    }{%
    }%
    %
    %
    \pgfplotsmanualhtmlmodetrue
    %
    \gdef\pgfsys@imagesuffixlist{.png}
    %
    \tikzifexternalizing{%
        %\nofiles
        \pgfkeys{/pdflinks/codeexample links=false}%
    }{}%
}

% explanation of how to customize tex4ht:
% http://www.cvr.cc/tex4ht-low-level-commands/#more-482
%
% some reference:
% http://www.cvr.cc/tex4ht-options/
%
% some tex4ht mailing list discussion:
% http://tug.org/pipermail/tex4ht/2010q4/000246.html
\def\patches@for@htlatex{%
 \let\savecolor\color
 \NewConfigure{color}[2]{\def\a at color{##1}\def\b at color{##2}}
 \def\@@tmp##1{\a at color##1\b at color\savecolor{##1}\aftergroup\endspan}
 \let\color\@@tmp
 \def\endspan{\Tg</span>}
 \Configure{color}{\HCode{<span style="color:}}{\HCode{;">}}
}%

\def\pgfplotsmanual@configure@for@htlatexmode@{%
    \NewConfigure{endofcodeexample}[2]{%
        \def\endofcodeexample@pre{##1}%
        \def\endofcodeexample@post{##2}%
    }%
    \let\endofcodeexample@orig=\endofcodeexample
    \def\endofcodeexample##1{%
        \endofcodeexample@pre
        \endofcodeexample@orig{##1}%
        \endofcodeexample@post
    }%
    \Configure{endofcodeexample}
        {\Tg<div class="img">}
        {\Tg</div>}%
    %
    \NewConfigure{pgfmanual@@typeset@listing}[2]{%
        \def\pgfmanual@@typeset@listing@pre{##1}%
        \def\pgfmanual@@typeset@listing@post{##2}%
    }%
    \let\pgfmanual@@typeset@listing@orig=\pgfmanual@@typeset@listing
    \def\pgfmanual@@typeset@listing##1{%
        \pgfmanual@@typeset@listing@pre
        \pgfmanual@@typeset@listing@orig{##1}%
        \pgfmanual@@typeset@listing@post
    }%
    \Configure{pgfmanual@@typeset@listing}
        {\Tg<div class="texsrc">}
        {\Tg</div>}%
    %
    %
    \NewConfigure{pgfmanual@codeexample@generate@graphic}[2]{%
        \def\pgfmanual@codeexample@generate@graphic@pre{##1}%
        \def\pgfmanual@codeexample@generate@graphic@post{##2}%
    }%
    \def\pgfmanual@codeexample@generate@graphic##1{%
        \pgfmanual@codeexample@generate@graphic@pre
        % the '@' variant skips the colorbox:
        \pgfmanual@codeexample@generate@graphic@{##1}%
        \pgfmanual@codeexample@generate@graphic@post
    }%
    \Configure{pgfmanual@codeexample@generate@graphic}
        {\Tg<span class="graphics">}
        {\Tg</span>}%
}

\@ifpackageloaded{tex4ht}{
    % always active here:
    \def\pgfplotsmanualhtlatexmode{1}%
}{
}

% the macro \pgfplotsmanualhtlatexmode should be set from command line
% (to any value). If it is known, the manual will be translated to
% HTML.
\pgfutil@ifundefined{pgfplotsmanualhtlatexmode}{%
}{%
    \pgfplotsmanual@configure@for@htlatexmode
}%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



\newenvironment{addplotoperation}[3][]{
  \begin{pgfmanualentry}
      {%
    \let\ltxdoc@marg=\marg
    \let\ltxdoc@oarg=\oarg
    \let\ltxdoc@parg=\parg
    \let\ltxdoc@meta=\meta
    \def\marg##1{{\normalfont\ltxdoc@marg{##1}}}%
    \def\oarg##1{{\normalfont\ltxdoc@oarg{##1}}}%
    \def\parg##1{{\normalfont\ltxdoc@parg{##1}}}%
    \def\meta##1{{\normalfont\ltxdoc@meta{##1}}}%
    \pgfmanualentryheadline{\textcolor{gray}{{\ttfamily\char`\\addplot\ }}%
      \declare{\texttt{#2}} \texttt{#3;}}%
      \unskip
     \nobreak
    \pgfmanualentryheadline{\textcolor{gray}{\texttt{\char`\\addplot}\oarg{options} }%
      \declare{\texttt{#2}} \texttt{#3} \textcolor{gray}{\meta{trailing path commands}}\texttt{;}}%
      \unskip
     \nobreak
    \pgfmanualentryheadline{\textcolor{gray}{{\ttfamily\char`\\addplot3}} $\dotsc$}%
    \def\pgfmanualtest{#1}%
    \ifx\pgfmanualtest\@empty%
      \index{#2@\protect\textcolor{gray}{\protect\texttt{plot}}\protect\texttt{ #2}}%
      \index{Plot operations!plot #2@\protect\texttt{plot #2}}%
    \fi%
    \pgfmanualpdflabel{\textbackslash addplot #2}{}%
    \pgfmanualpdflabel{plot #2}{}%
    \pgfmanualpdflabel{#2}{}%
    }%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}

\newenvironment{addplot+}{
  \begin{pgfmanualentry}
      {%
    \let\ltxdoc@marg=\marg
    \let\ltxdoc@oarg=\oarg
    \let\ltxdoc@parg=\parg
    \let\ltxdoc@meta=\meta
    \def\marg##1{{\normalfont\ltxdoc@marg{##1}}}%
    \def\oarg##1{{\normalfont\ltxdoc@oarg{##1}}}%
    \def\parg##1{{\normalfont\ltxdoc@parg{##1}}}%
    \def\meta##1{{\normalfont\ltxdoc@meta{##1}}}%
    \pgfmanualentryheadline{{\ttfamily\declare{\char`\\addplot+}\oarg{options} \textcolor{gray}{\dots};}}%
    \index{addplot+@\protect\texttt{\protect\textbackslash addplot+}}%
    \pgfmanualpdflabel{\textbackslash addplot+}{}%
    }%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}
\newenvironment{addplot3generic}{
  \begin{pgfmanualentry}
      {%
    \let\ltxdoc@marg=\marg
    \let\ltxdoc@oarg=\oarg
    \let\ltxdoc@parg=\parg
    \let\ltxdoc@meta=\meta
    \def\marg##1{{\normalfont\ltxdoc@marg{##1}}}%
    \def\oarg##1{{\normalfont\ltxdoc@oarg{##1}}}%
    \def\parg##1{{\normalfont\ltxdoc@parg{##1}}}%
    \def\meta##1{{\normalfont\ltxdoc@meta{##1}}}%
    \pgfmanualentryheadline{{\ttfamily\declare{\char`\\addplot3}\oarg{options} \meta{input data} \meta{trailing path commands};}}%
    \index{addplot3@\protect\texttt{\protect\textbackslash addplot3}}%
    \pgfmanualpdflabel{\textbackslash addplot3}{}%
    }%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}
\newenvironment{addplot3operation}[3][]{
  \begin{pgfmanualentry}
      {%
    \let\ltxdoc@marg=\marg
    \let\ltxdoc@oarg=\oarg
    \let\ltxdoc@parg=\parg
    \let\ltxdoc@meta=\meta
    \def\marg##1{{\normalfont\ltxdoc@marg{##1}}}%
    \def\oarg##1{{\normalfont\ltxdoc@oarg{##1}}}%
    \def\parg##1{{\normalfont\ltxdoc@parg{##1}}}%
    \def\meta##1{{\normalfont\ltxdoc@meta{##1}}}%
    \pgfmanualentryheadline{\textcolor{gray}{{\ttfamily\char`\\addplot3\ }}%
      \declare{\texttt{#2}} \texttt{#3;}}%
      \unskip
     \nobreak
    \pgfmanualentryheadline{\textcolor{gray}{\texttt{\char`\\addplot3}\oarg{options} }%
      \declare{\texttt{#2}} \texttt{#3} \textcolor{gray}{\meta{trailing path commands}}\texttt{;}}%
    \def\pgfmanualtest{#1}%
    \ifx\pgfmanualtest\@empty%
      \index{#2@\protect\texttt{#2}}%
      \index{Plot operations!addplot3 #2@\protect\texttt{#2}}%
    \fi%
    \pgfmanualpdflabel{\textbackslash addplot3 #2}{}%
    \pgfmanualpdflabel{plot3 #2}{}%
    }%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}

\newenvironment{codekey}[1]{%
  \begin{pgfmanualentry}
     \pgfmanualentryheadline{{\ttfamily\declarekey{#1}\textcolor{gray}{/\pgfmanualpdfref{/handlers/.code}{.code}}=\marg{...}}\hfill}%
    \def\mykey{#1}%
    \def\mypath{}%
    \def\myname{}%
    \firsttimetrue%
    \decompose#1/\nil%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}
\newenvironment{codeargskey}[2]{%
  \begin{pgfmanualentry}
      {\toks0={#2}%
      \xdef\argpattern{\the\toks0 }%
    }%
      \pgfmanual@command@to@string\argpattern\argpattern
     \pgfmanualentryheadline{{\ttfamily\declarekey{#1}\textcolor{gray}{/\pgfmanualpdfref{/handlers/.code}{.code args}}=\texttt{\{\argpattern\}}\marg{...}}\hfill}%
    \def\mykey{#1}%
    \def\mypath{}%
    \def\myname{}%
    \firsttimetrue%
    \decompose#1/\nil%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}
\def\pgfmanual@command@to@string#1#2{%
    \expandafter\pgfmanual@command@to@string@@\meaning#1\pgfmanual@EOI{#2}%
}%
\xdef\pgfmanual@glob@TMPa{\meaning\pgfutil@empty}%
\expandafter\def\expandafter\pgfmanual@command@to@string@@\pgfmanual@glob@TMPa#1\pgfmanual@EOI#2{%
    \def#2{#1}%
}%

\newenvironment{pgfplotscodekey}[1]{%
    \begin{codekey}{/pgfplots/#1}%
}
{
  \end{codekey}
}
\newenvironment{pgfplotscodetwokey}[1]{%
  \begin{pgfmanualentry}
     \pgfmanualentryheadline{{\ttfamily\declarekey{/pgfplots/#1}\textcolor{gray}{/\pgfmanualpdfref{/handlers/.code 2 args}{.code 2 args}}=\marg{...}}\hfill}%
    \def\mykey{/pgfplots/#1}%
    \def\mypath{}%
    \def\myname{}%
    \firsttimetrue%
    \decompose/pgfplots/#1/\nil%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}

\newenvironment{pgfplotsxycodekeylist}[1]{%
    \begingroup
    \let\oldpgfmanualentryheadline=\pgfmanualentryheadline
    \def\pgfmanualentryheadline##1{%
        \pgfmanualentryheadline@##1\pgfplots@EOI
    }%
    \def\pgfmanualentryheadline@##1\hfill##2\pgfplots@EOI{%
        \oldpgfmanualentryheadline{{\ttfamily\declarekey{##1}\textcolor{gray}{/\pgfmanualpdfref{/handlers/.code}{.code}}=\marg{...}}\hfill}%
    }
    \begin{pgfplotsxykeylist}{#1}%
}
{
    \end{pgfplotsxykeylist}
    \endgroup
}

\newenvironment{pgfplotsxyexpressionkeylist}[1]{%
    \begingroup
    \let\oldpgfmanualentryheadline=\pgfmanualentryheadline
    \def\pgfmanualentryheadline##1{%
        \pgfmanualentryheadline@##1\pgfplots@EOI
    }%
    \def\pgfmanualentryheadline@##1\hfill##2\pgfplots@EOI{%
        \oldpgfmanualentryheadline{{\ttfamily\declarekey{##1}\textcolor{gray}{/.expression}=\marg{math expression}}\hfill}%
    }
    \begin{pgfplotsxykeylist}{#1}%
}
{
    \end{pgfplotsxykeylist}
    \endgroup
}
\newenvironment{pgfplotsxyappendexpressionkeylist}[1]{%
    \begingroup
    \let\oldpgfmanualentryheadline=\pgfmanualentryheadline
    \def\pgfmanualentryheadline##1{%
        \pgfmanualentryheadline@##1\pgfplots@EOI
    }%
    \def\pgfmanualentryheadline@##1\hfill##2\pgfplots@EOI{%
        \oldpgfmanualentryheadline{{\ttfamily\declarekey{##1}\textcolor{gray}{/.append expression}=\marg{math expression}}\hfill}%
    }
    \begin{pgfplotsxykeylist}{#1}%
}
{
    \end{pgfplotsxykeylist}
    \endgroup
}

\newenvironment{pgfplotskey}[1]{%
  \begin{key}{/pgfplots/#1}%
}
{
  \end{key}
}

\newenvironment{tikzkey}[1]{%
  \begin{key}{/tikz/#1}%
}
{
  \end{key}
}

\def\choicesep{$\vert$}%
\def\choicearg#1{\texttt{#1}}

\newif\iffirstchoice
\newcommand\mchoice[1]{%
    \begingroup
    \let\margold=\marg
    \def\marg##1{{\normalfont\margold{##1}}}%
    \firstchoicetrue
    \foreach \mchoice@ in {#1} {%
        \iffirstchoice
            \global\firstchoicefalse
        \else
            \choicesep
        \fi
        \choicearg{\mchoice@}%
    }%
    \endgroup
}%




% \begin{xykey}{/path/\x label=value}
% \end{xykey}
%
% has same features with 'default', 'initially' etc as key environment
\newenvironment{xykey}[2][]{%
    \begin{pgfmanualentry}
    \def\extrakeytext{}
    \insertpathifneeded{#2}{#1}%
    \expandafter\pgfutil@in@\expandafter=\expandafter{\mykey}%
    \ifpgfutil@in@%
        \expandafter\xykey@eq\mykey\@nil
    \else
        \expandafter\xykey@noeq\mykey\@nil
    \fi
    \pgfmanualbody
}{%
    \end{pgfmanualentry}
}%

% \begin{xystylekey}{/path/\x label=value}
% \end{xystylekey}
%
% has same features with 'default', 'initially' etc as key environment
\newenvironment{xystylekey}[2][]{%
    \begin{pgfmanualentry}
    \def\extrakeytext{style, }
    \insertpathifneeded{#2}{#1}%
    \expandafter\pgfutil@in@\expandafter=\expandafter{\mykey}%
    \ifpgfutil@in@%
        \expandafter\xykey@eq\mykey\@nil
    \else
        \expandafter\xykey@noeq\mykey\@nil
    \fi
    \pgfmanualbody
}{%
    \end{pgfmanualentry}
}%

% \insertpathifneeded{a key}{/pgfplots} -> assign mykey={/pgfplots/a key}
% \insertpathifneeded{/tikz/a key}{/pgfplots} -> assign mykey={/tikz/a key}
%
% #1: the key
% #2: a default path (or empty)
\def\insertpathifneeded#1#2{%
    \def\insertpathifneeded@@{#2}%
    \ifx\insertpathifneeded@@\empty
        \def\mykey{#1}%
    \else
        \insertpathifneeded@#1\@nil
        \ifpgfutil@in@
            \def\mykey{#1}%
        \else
            \def\mykey{#2/#1}%
        \fi
    \fi
}%
\def\insertpathifneeded@#1#2\@nil{%
    \def\insertpathifneeded@@{#1}%
    \def\insertpathifneeded@@@{/}%
    \ifx\insertpathifneeded@@\insertpathifneeded@@@
        \pgfutil@in@true
    \else
        \pgfutil@in@false
    \fi
}%

% \begin{keylist}[default path]
%     {/path/option 1=value,/path/option 2=value2}
% \end{keylist}
\newenvironment{keylist}[2][]{%
    \begin{pgfmanualentry}
    \def\extrakeytext{}%
    \foreach \xx in {#2} {%
        \expandafter\insertpathifneeded\expandafter{\xx}{#1}%
        \expandafter\extractkey\mykey\@nil%
    }%
    \pgfmanualbody
}{%
  \end{pgfmanualentry}
}%

\newenvironment{pgfplotskeylist}[1]{%
    \begin{keylist}[/pgfplots]{#1}%
}{%
    \end{keylist}%
}

\newenvironment{tikzkeylist}[1]{%
    \begin{keylist}[/tikz]{#1}%
}{%
    \end{keylist}%
}

\newenvironment{anchorlist}[1]{
  \begin{pgfmanualentry}
      \foreach \xx in {#1} {%
        \pgfmanualentryheadline{Anchor {\ttfamily\declare{\xx}}}%
        \index{\xx @\protect\texttt{\xx} anchor}%
        \index{Anchors!\xx @\protect\texttt{\xx}}
        \expandafter\pgfmanualpdflabel\expandafter{\xx}{}
    }%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}

\newenvironment{coordinatesystemlist}[1]{
  \begin{pgfmanualentry}
      \foreach \xx in {#1} {%
        \pgfmanualentryheadline{Coordinate system {\ttfamily\declare{\xx}}}%
        \index{\xx @\protect\texttt{\xx} coordinate system}%
        \index{Coordinate systems!\xx @\protect\texttt{\xx}}
        \expandafter\pgfmanualpdflabel\expandafter{\xx}{}
    }%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}
\renewenvironment{coordinatesystem}[1]{
  \begin{pgfmanualentry}
    \pgfmanualentryheadline{Coordinate system {\ttfamily\declare{#1}}}%
    \index{#1@\protect\texttt{#1} coordinate system}%
    \index{Coordinate systems!#1@\protect\texttt{#1}}
    \pgfmanualpdflabel{#1}{}
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}

% \begin{xykeylist}[default path]
%     {/path/option \x1=value,/path/option \x2=value2,/path/option \x3=value}
% \end{xykeylist}
\newenvironment{xykeylist}[2][]{%
    \begin{pgfmanualentry}
    \def\extrakeytext{}
    \foreach \xx in {#2} {%
        \expandafter\insertpathifneeded\expandafter{\xx}{#1}%
        \expandafter\pgfutil@in@\expandafter=\expandafter{\mykey}%
        \ifpgfutil@in@%
            \expandafter\xykey@eq\mykey\@nil
        \else
            \expandafter\xykey@noeq\mykey\@nil
        \fi
    }%
    \pgfmanualbody
}{%
  \end{pgfmanualentry}
}%

\makeatother % FIXME this is almost surely a bug in pgfmanual-en-macros
% \begin{commandlist}
%     {\command1{arg1},\command2{\arg2}}
% \end{commandlist}
\newenvironment{commandlist}[1]{%
    \begin{pgfmanualentry}
    \foreach \xx in {#1} {%
        \expandafter\extractcommand\xx\@@%
    }%
    \pgfmanualbody
}{%
  \end{pgfmanualentry}
}%

\newenvironment{texif}[1]{%
    \begin{pgfmanualentry}
    \pgfmanualentryheadline{\declare{\texttt{\textbackslash if#1}}\meta{true code}\texttt{\textbackslash else}\meta{else code}\texttt{\textbackslash fi}}%
    \index{if#1}%
    \pgfmanualpdflabel{\\if#1}{}%
    \pgfmanualbody
}{%
  \end{pgfmanualentry}
}%
\makeatletter

\newif\ifxykeyfound

\def\pgfmanual@xykey@install@replacements{%
    \def\ { }%
    \def\space{ }%
}%

\def\xykey@eq#1=#2\@nil{%
    \begingroup
    \pgfmanual@xykey@install@replacements
    \def\x{x}%
    \xdef\mykey{#1}%
    \def\xykey@@{#1}%
    \ifx\xykey@@\mykey
        \xykeyfoundfalse
    \else
        \xykeyfoundtrue
    \fi
    \expandafter\extractkey\mykey=#2\@nil%
    \ifxykeyfound
        \def\x{y}%
        \xdef\mykey{#1}%
        \expandafter\extractkey\mykey=#2\@nil%
        \def\x{z}%
        \xdef\mykey{#1}%
        \expandafter\extractkey\mykey=#2\@nil%
    \fi
    \endgroup
}
\def\xykey@noeq#1\@nil{%
    \begingroup
    \pgfmanual@xykey@install@replacements
    \def\x{x}%
    \xdef\mykey{#1}%
    \def\xykey@@{#1}%
    \ifx\xykey@@\mykey
        \xykeyfoundfalse
    \else
        \xykeyfoundtrue
    \fi
    \expandafter\extractkey\mykey\@nil%
    \ifxykeyfound
        \def\x{y}%
        \xdef\mykey{#1}%
        \expandafter\extractkey\mykey\@nil%
        \def\x{z}%
        \xdef\mykey{#1}%
        \expandafter\extractkey\mykey\@nil%
    \fi
    \endgroup
}

% \begin{pgfplotsxykey}{\x label=value}
% \end{pgfplotsxykey}
%
% It introduces the path /pgfplots/ automatically.
%
% has same features with 'default', 'initially' etc as key environment
\newenvironment{pgfplotsxykey}[1]{%
    \begin{xykey}[/pgfplots]{#1}%
}{%
    \end{xykey}%
}


\newenvironment{pgfplotsxykeylist}[1]{%
    \begin{xykeylist}[/pgfplots]{#1}%
}{%
    \end{xykeylist}%
}


% the first, optional argument is the default key path to insert.
\newenvironment{plottype}[2][/tikz]{%
    \begin{keylist}[#1]{#2}%
    \end{keylist}
  \begin{pgfmanualentry}
    \pgfmanualentryheadline{\textcolor{gray}{{\ttfamily\char`\\addplot+[\declare{#2}]}}}%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}

\def\index@prologue{\section*{Index}\addcontentsline{toc}{section}{Index}
}

\newenvironment{pgfplotstablecolumnkey}{%
  \begin{pgfmanualentry}
     \pgfmanualentryheadline{{\ttfamily\textcolor{gray}{/pgfplots/table/}\declare{columns/\meta{column name}}\textcolor{gray}{/.style}=\marg{key-value-list}}\hfill}%
    \pgfplotsmanualkeyindex{/pgfplots/table/columns}%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}
\newenvironment{pgfplotstabledisplaycolumnkey}{%
  \begin{pgfmanualentry}
     \pgfmanualentryheadline{{\ttfamily\textcolor{gray}{/pgfplots/table/}\declare{display columns/\meta{index}}\textcolor{gray}{/.style}=\marg{key-value-list}}\hfill}%
    \pgfplotsmanualkeyindex{/pgfplots/table/display columns}%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}
\newenvironment{pgfplotstablealiaskey}{%
  \begin{pgfmanualentry}
     \pgfmanualentryheadline{{\ttfamily\textcolor{gray}{/pgfplots/table/}\declare{alias/\meta{col name}}\textcolor{gray}{/.initial}=\marg{real col name}}\hfill}%
    \pgfplotsmanualkeyindex{/pgfplots/table/alias}%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}


\def\pgfplotsmanualkeyindex#1{%
    \def\mypath{#1}%
    \def\myname{}%
    \firsttimetrue%
    \decompose#1/\nil%
}
\newenvironment{pgfplotstablecreateonusekey}{%
  \begin{pgfmanualentry}
     \pgfmanualentryheadline{{\ttfamily\textcolor{gray}{/pgfplots/table/}\declare{create on use/\meta{col name}}\textcolor{gray}{/.style}=\marg{create options}}\hfill}%
    \def\mykey{/pgfplots/table/create on use}%
    \pgfmanualbody
    \pgfplotsmanualkeyindex{/pgfplots/table/create on use}%
}
{
  \end{pgfmanualentry}
}

\def\pgfplotsassertcmdkeyexists#1{%
    \pgfkeysifdefined{/pgfplots/#1/.@cmd}\relax{%
        \pgfplots@error{DOCUMENTATION ERROR: command key /pgfplots/#1 does not exist!}%
    }%
}%

{
\catcode`\ =12%
\gdef\makespaceexpandable{\def\ { }}}%

\def\pgfplotsassertXYcmdkeyexists#1{%
    {\makespaceexpandable\def\x{x}\edef\pgfplotsassertXYcmdkeyexists@tmp{#1}%
    \pgfkeysifdefined{/pgfplots/\pgfplotsassertXYcmdkeyexists@tmp/.@cmd}\relax{%
        \pgfplots@error{DOCUMENTATION ERROR: command key /pgfplots/#1 does not exist!}%
    }}%
    {\makespaceexpandable\def\x{y}\edef\pgfplotsassertXYcmdkeyexists@tmp{#1}%
    \pgfkeysifdefined{/pgfplots/\pgfplotsassertXYcmdkeyexists@tmp/.@cmd}\relax{%
        \pgfplots@error{DOCUMENTATION ERROR: command key /pgfplots/#1 does not exist!}%
    }}%
}%

\def\pgfplotsshortstylekey #1=#2\pgfeov{%
    \pgfplotsassertcmdkeyexists{#1}%
    \pgfplotsassertcmdkeyexists{#2}%
    \begin{pgfplotskey}{#1=\marg{key-value-list}}
        An abbreviation for \texttt{\pgfmanualpdfref{#2}{#2}/\pgfmanualpdfref{/handlers/.append style}{.append style}=}\marg{key-value-list}.

        It appends options to the already existing style \texttt{\pgfmanualpdfref{#2}{#2}}.
    \end{pgfplotskey}
}
\def\pgfplotsshortxystylekey #1=#2\pgfeov{%
    \pgfplotsassertXYcmdkeyexists{#1}%
    \pgfplotsassertXYcmdkeyexists{#2}%
    \begin{pgfplotsxykey}{#1=\marg{key-value-list}}
        An abbreviation for {\def\x{x}\texttt{\pgfmanualpdfref{#2}{#2}/\pgfmanualpdfref{/handlers/.append style}{.append style}=}}\marg{key-value-list}
        (or the respective styles for $y$,
            {\def\x{y}\texttt{\pgfmanualpdfref{#2}{#2}/\pgfmanualpdfref{/handlers/.append style}{.append style}=}}\marg{key-{}value-{}list},
        and the $z$-axis,
            {\def\x{z}\texttt{\pgfmanualpdfref{#2}{#2}/\pgfmanualpdfref{/handlers/.append style}{.append style}=}}\marg{key-{}value-{}list}).

        It appends options to the already existing style {\def\x{x}\texttt{\pgfmanualpdfref{#2}{#2}}}.
    \end{pgfplotsxykey}
}
\def\pgfplotsshortstylekeys #1,#2=#3\pgfeov{%
    \pgfplotsassertcmdkeyexists{#1}%
    \pgfplotsassertcmdkeyexists{#2}%
    \pgfplotsassertcmdkeyexists{#3}%
    \begin{pgfplotskeylist}{%
        #1=\marg{key-value-list},
        #2=\marg{key-value-list}}
        Different abbreviations for \texttt{\pgfmanualpdfref{#3}{#3}/\pgfmanualpdfref{/handlers/.append style}{.append style}=}\marg{key-value-list}.
    \end{pgfplotskeylist}
}
\def\pgfplotsshortxystylekeys #1,#2=#3\pgfeov{%
    \pgfplotsassertXYcmdkeyexists{#1}%
    \pgfplotsassertXYcmdkeyexists{#2}%
    \pgfplotsassertXYcmdkeyexists{#3}%
    \begin{pgfplotsxykeylist}{%
        #1=\marg{key-value-list},
        #2=\marg{key-value-list}}
        Different abbreviations for {\def\x{x}\texttt{\pgfmanualpdfref{#3}{#3}/\pgfmanualpdfref{/handlers/.append style}{.append style}=}}\marg{key-value-list}
        (or the respective styles for $y$,
            {\def\x{y}\texttt{\pgfmanualpdfref{#3}{#3}/\pgfmanualpdfref{/handlers/.append style}{.append style}=}\marg{key-{}value-{}list}}, and $z$,
            {\def\x{z}\texttt{\pgfmanualpdfref{#3}{#3}/\pgfmanualpdfref{/handlers/.append style}{.append style}=}\marg{key-{}value-{}list}}%
            ).
    \end{pgfplotsxykeylist}
}


%
% For using the correct form of including libraries in the manual.
%
\newenvironment{pgfplotslibrary}[1]{%
  \begin{pgfmanualentry}
    \pgfmanualentryheadline{{\ttfamily\char`\\usepgfplotslibrary\char`\{\declare{#1}\char`\}\space\space \char`\%\space\space  \LaTeX\space and plain \TeX}}%
    \index{#1@\protect\texttt{#1} library}%
    \index{Libraries!#1@\protect\texttt{#1}}%
    \pgfmanualentryheadline{{\ttfamily\char`\\usepgfplotslibrary[\declare{#1}]\space \char`\%\space\space Con\TeX t}}%
    \pgfmanualentryheadline{{\ttfamily\char`\\usetikzlibrary\char`\{\declare{pgfplots.#1}\char`\}\space\space \char`\%\space\space \LaTeX\space and plain \TeX}}%
    \pgfmanualentryheadline{{\ttfamily\char`\\usetikzlibrary[\declare{pgfplots.#1}]\space \char`\%\space\space Con\TeX t}}%
    \pgfmanualpdflabel{#1}{}%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}


%
% Creates and shows a colormap with specification '#1'.
\def\pgfplotsshowcolormapexample#1{%
    \pgfplotscreatecolormap{tempcolormap}{#1}%
    \pgfplotsshowcolormap{tempcolormap}%
}

% Shows the colormap named '#1'.
\def\pgfplotsshowcolormap#1{%
    \pgfplotscolormapifdefined{#1}{\relax}{%
        \pgfplotsset{colormap/#1}%
    }%
    \pgfplotscolormaptoshadingspec{#1}{8cm}\result
    \def\tempb{\pgfdeclarehorizontalshading{tempshading}{1cm}}%
    \expandafter\tempb\expandafter{\result}%
    \pgfuseshading{tempshading}%
}

\makeatother

\def\decompose/#1/#2\nil{%
  \def\test{#2}%
  \ifx\test\empty%
    % aha.
    \index{#1@\protect\texttt{#1} key}%
    \ifx\mypath\empty
    \else
        \index{\mypath#1@\protect\texttt{#1}}%
    \fi
    \def\myname{#1}%
    %\pgfmanualpdflabel{#1}{}% No, its better to use fully qualified keys and search if necessary!
  \else%
    \iffirsttime
        \begingroup
            % also make a pdf link anchor with full key path.
            \def\hyperlabelwithoutslash##1/\nil{%
                \pgfmanualpdflabel{##1}{}%
            }%
            \hyperlabelwithoutslash/#1/#2\nil
        \endgroup
%    CF : disabled for /pgfplots/ prefix.
%        \def\mypath{#1@\protect\texttt{/#1/}!}%
%        \firsttimefalse
        \def\pgfplotslocTMPa{pgfplots}%
        \edef\pgfplotslocTMPb{#1}%
        \ifx\pgfplotslocTMPb\pgfplotslocTMPa
            \def\mypath{}%
        \else
            \def\mypath{#1@\protect\texttt{/#1/}!}%
        \fi
        \firsttimefalse
    \else
      \expandafter\def\expandafter\mypath\expandafter{\mypath#1@\protect\texttt{#1/}!}%
    \fi
    \def\firsttime{}
    \decompose/#2\nil%
  \fi%
}
\def\extracthandler#1#2\@nil{%
  \pgfmanualentryheadline{Key handler \meta{key}{\ttfamily/\declare{#1}}#2}%
  \index{\gobble#1@\protect\texttt{#1} handler}%
  \index{Key handlers!#1@\protect\texttt{#1}}
  \pgfmanualpdflabel{/handlers/#1}{}%
}
\def\extractcommand#1#2\@@{%
  \pgfmanualentryheadline{\declare{\texttt{\string#1}}#2}%
  \removeats{#1}%
  \index{\strippedat @\protect\myprintocmmand{\strippedat}}%
  \pgfmanualpdflabel{\textbackslash\strippedat}{}%
}
\def\extractenvironement#1#2\@@{%
  \pgfmanualentryheadline{{\ttfamily\char`\\begin\char`\{\declare{#1}\char`\}}#2}%
  \pgfmanualentryheadline{{\ttfamily\ \ }\meta{environment contents}}%
  \pgfmanualentryheadline{{\ttfamily\char`\\end\char`\{\declare{#1}\char`\}}}%
  \index{#1@\protect\texttt{#1} environment}%
  \index{Environments!#1@\protect\texttt{#1}}%
  \pgfmanualpdflabel{#1}{}%
}
\renewenvironment{predefinednode}[1]{
  \begin{pgfmanualentry}
    \pgfmanualentryheadline{Predefined node {\ttfamily\declare{#1}}}%
    \index{#1@\protect\texttt{#1} node}%
    \index{Predefined node!#1@\protect\texttt{#1}}
    \pgfmanualpdflabel{#1}{}%
    \pgfmanualbody
}
{
  \end{pgfmanualentry}
}
